// 配置
layui.config({
    base: '/static/js/'  // 模块目录
}).extend({                     // 模块别名
    m_table: 'm_table'
});

// layui方法
layui.use(['table', 'm_table'], function () {

    // 操作对象
    var table = layui.table
        , $ = layui.jquery
        , mTable = layui.m_table;

    // 表格渲染
    var tableIns = table.render({
        elem: '#dataTable'                  //指定原始表格元素选择器（推荐id选择器）
        , height: 'auto'  //容器高度
        , cols: [[                  //标题栏
            {field: 'userName', title: '角色名称',width: 250, align: 'center',}
            ,{
                field: 'userEnable', title: '角色状态', width: 150, align: 'center', templet: function (d) {
                    if (d.userEnable === 0) {
                        return '<a lay-data = ' + d.id + ' class="layui-btn layui-btn-sm layui-btn-warm" style="background-color: #2B2E37" lay-event="usable">已禁用</a>';
                    } else if (d.userEnable === 1) {
                        return '<a lay-data = ' + d.id + ' class="layui-btn layui-btn-sm layui-btn-warm" lay-event="usable">已启用</a>';
                    }
                }
            }
            , {title: '操作', width: 255, templet: '#roleListBar', fixed: "right", align: "center",}
        ]]
        , id: 'dataCheck'
        , url: '/user/listData'
        , method: 'get'
        , page: false
        , loading: true
    });

    $("#add").click(function () {
        layer.open({
            title: '添加用户',
            maxmin: true,
            type: 2,
            content: '/user/add',
            area: ['450px', '330px'],
            end: function () {
                tableIns.reload();
            }
        });
    });

    //列表操作
    table.on('tool(dataTable)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;

        if (layEvent === 'rename') { //编辑
            layer.prompt({
                formType: 2,
                value: data.roleDesc,
                title: '请输入新的名称',
                area: ['170px', '60px'] //自定义文本域宽高
            }, function (value, index, elem) {
                $.ajax({
                    type: "post",
                    url: "/user/rename",
                    data: {"userId": data.id, "newName": value},
                    success: function (data) {
                        layer.close(index);
                        tableIns.reload();
                    }, error: function (xhr, status, error) {
                        layer.msg("服务器异常")
                    }
                })
            });
        } else if (layEvent === 'edit') {
            layer.open({
                title: '角色管理',
                maxmin: true,
                type: 2,
                content: '/user/edit/' + data.id,
                area: ['450px', '280px'],
                end: function () {

                }
            });
        } else if (layEvent === 'usable') { //启用禁用
            var _this = $(this),
                usableText = "是否确定禁用此用户？",
                btnText = "已禁用",
                status = 0,
                userId = _this.attr("lay-data");
            if (_this.text() == "已禁用") {
                usableText = "是否确定启用此用户？";
                    btnText = "已启用";
                    status = 1;
            }
            layer.confirm(usableText, {
                icon: 3,
                title: '系统提示',
                cancel: function (index) {
                    layer.close(index);
                }
            }, function (index) {
                $.ajax({
                    type: "delete",
                    url: "/user/updateStatus",
                    data: {"userId": userId, "status": status},
                    dataType: "json",
                    success: function (data) {
                        if (data.status === 200) {
                            _this.text(btnText);
                            if(status === 1){
                                _this.css("background-color","#FFB800")
                            }else if(status === 0){
                                _this.css("background-color","#2B2E37")
                            }
                            //tableIns.reload();
                            layer.close(index);
                        } else {
                            layer.alert(data.message, {icon: 0})
                        }
                    }, error: function (xhr, status, error) {
                        layer.msg("服务器异常")
                    }
                });
            }, function (index) {
                layer.close(index);
            });
        } else if (layEvent === 'del') { //删除
            layer.confirm('确定删除此用户？', {icon: 3, title: '提示信息'}, function (index) {
                $.ajax({
                    type: "delete",
                    url: "/user/delete/" + data.id,
                    dataType: "json",
                    success: function (data) {
                        if (data.status === 200) {
                            tableIns.reload();
                            layer.close(index);
                        } else {
                            layer.alert(data.message, {icon: 0})
                        }
                    }, error: function (xhr, status, error) {
                        layer.msg("服务器异常")
                    }
                });
            });
        }
    });
});